#include "Plugin.h"
#include <fstream>

Plugin *Plugin::loadPlugin(string fileName) {
    HMODULE lib = LoadLibrary(fileName.c_str());
    if (lib == NULL) {
        return NULL;
    }

    const char *name = (const char*) GetProcAddress(lib, "_name");
    if (name == NULL) {
        return NULL;
    }

    
    GetType gt = (GetType)GetProcAddress(lib, "_getType");
    if (gt == NULL) {
        return NULL;
    }
    
    HasTime ht = (HasTime)GetProcAddress(lib, "_hasTime");
    if (ht == NULL) {
        return NULL;
    }

    RemoveTime rt = (RemoveTime)GetProcAddress(lib, "_removeTime");
    if (rt == NULL) {
        return NULL;
    }

    return new Plugin(lib, name, gt, ht, rt);
}


Plugin::Plugin(HMODULE lib, const char *name, GetType gt, HasTime ht, RemoveTime rt) {
    this->name = name;
    this->lib = lib;
    this->getTypeFunc = gt;
    this->hasTimeFunc = ht;
    this->removeTimeFunc = rt;
}

Plugin::~Plugin() {
    FreeLibrary(lib);
}

const char *Plugin::getName() const {
    return name;
}

MessageType Plugin::getType(const char *msg) const {
    return this->getTypeFunc(msg);
}
bool Plugin::hasTime(const char *msg) const {
    return this->hasTimeFunc(msg);
}
string Plugin::removeTime(const char *msg) const {
    return this->removeTimeFunc(msg);
}
